Ext.define('PJ.view.unitregistrationmanagement.ChooseUserList', {
    extend: 'AM.default.DefaultList',
	alias: 'widget.ChooseUserList',
    
    initComponent: function(){
    	var me = this;
    	if (me.clickSource == 2) {
	        this.columns = [{
	            header: '审核状态',
	            dataIndex: 'c.auditing_State',
	            width: 75,
	            //sortable: false,
	            renderer: function(v, meta, record){
	            	meta.style="padding-bottom: 0px;padding-top: 0px;line-height: 19px;padding-left: 0px;";
	                var text = "",tipText="";
	                if (v == 3) {
	                	tipText="报名待审核";
	                	text= '<div style="border-left:5px solid white;height:19px;padding-left:2px;">报名待审核</div>';
	                }
	                else if (v == 1) {
	                	text= '<div style="border-left:5px solid green;height:19px;padding-left:2px;">报名通过</div>';
	                	tipText="报名通过";
	                }else if (v == 2) {
	                	tipText="点击查看驳回原因";
	                	text= '<div style="border-left:5px solid red;height:19px;cursor: pointer;padding-left:2px;"><span style="cursor: pointer;color:blue;">报名未通过</span></div>';
	                }else if(v == 4){
	                	tipText="撤销待审核";
	                	text= '<div style="border-left:5px solid white;height:19px;padding-left:2px;">撤销待审核</div>';
	                }else if(v == 5){
	                	tipText="报名通过(撤销未通过)";
	                	text= '<div style="border-left:5px solid green;height:19px;padding-left:2px;">报名通过(<span style="cursor: pointer;color:blue;">撤销未通过</span>)</div>';
	                }
	                meta.tdAttr = 'data-qtip="' + tipText + '" auditStatus=' + record.get("c.auditing_State") + ' classUserId=' + record.get("classUserId") + ' userId=' + record.get("userId");
	                return text;
	            },
	    		listeners: {
	    			click: function(g, clm){
//	    				var userId = clm.getAttribute("userId");
	    				var auditStatus = clm.getAttribute("auditStatus");
	    				var classUserId = clm.getAttribute("classUserId");
	    				if(auditStatus == 5){
	    					var win = Ext.create('PJ.view.registrationmanagement.CheckEdit', {
	        					title: "查看驳回原因",
	        					flag:4,
	        					grid: me,
	        					isDisable:false,
	        					isReadOnly:true
	        				}).show();
	        				var actionName = "load";
	        				if(gj_onlineLearning_constant.loadType == "jsonp"){
	        					actionName = "loadJsonp";
	        				}
	        				win.down("form").getForm().doAction(actionName,{
	        					url:me.store.findRejectReasonUrl,
	        					params:{
	        						classUserId: classUserId
	        					},
	        					success:function(f,action){
	        					}
	        				});
	    				}else if(auditStatus == 2){
	    					var win = Ext.create('PJ.view.trainingmanagement.CheckEdit', {
	        					title: "查看驳回原因",
	        					flag:4,
	        					grid: me,
	        					isDisable:false,
	        					isReadOnly:true
	        				}).show();
	        				var actionName = "load";
	        				if(gj_onlineLearning_constant.loadType == "jsonp"){
	        					actionName = "loadJsonp";
	        				}
	        				win.down("form").getForm().doAction(actionName,{
	        					url:me.store.findCommentUrl,
	        					params:{
	        						classUserId: classUserId
	        					},
	        					success:function(f,action){
	        					}
	        				});
	    				}else{
	    					return;
	    				}
	    			}
	    		}
	        },{
	        	header: '学员帐号',
	        	dataIndex: 'bu.login_id',
	        	width: 75
	        },{
	        	header: '姓名',
	        	dataIndex: 'bu.user_Name',
	        	width: 70
	        }, {
	            header: '性别',
	            dataIndex: 'bb.order_num',
	            width: 45
	        },{
	            header: '出生年月',
	            dataIndex: 'c.birthday',
	            width: 70,
	            renderer: function(value, meta, record){
	                meta.tdAttr = 'data-qtip="' + value + '"';
	                return value;
	            }
	        }, {
	            header: '民族',
	            dataIndex: 'bt.order_num',
	            width: 50,
	            renderer: function(value, meta, record){
	                meta.tdAttr = 'data-qtip="' + value + '"';
	                return value;
	            }
	        },{
	            header: '手机号',
	            dataIndex: 'c.mobile_Telephone',
//	            hidden:true,
	            width: 100
	        }, /*{
	            header: '单位',
	            dataIndex: 'd.depname',
	            flex:1,
	            minWidth: 120,
	            renderer: function(value, meta, record){
	                meta.tdAttr = 'data-qtip="' + value + '"';
	                return value;
	            }
	        },*/ {
	            header: '职级',
	            dataIndex: 'bbb.order_num',
	            flex:.55,
	            renderer: function(value, meta, record){
	                meta.tdAttr = 'data-qtip="' + value + '"';
	                return value;
	            }
	        }, {
	            header: '单位及职务',
	            dataIndex: 'c.duty_remark',
	            flex:1,
	            renderer: function(value, meta, record){
	                meta.tdAttr = 'data-qtip="' + value + '"';
	                return value;
	            }
	        },/* {
	            header: '联系电话',
	            dataIndex: 'c.mobile_Telephone',
	            flex:1,
	            minWidth: 120,
	            renderer: function(value, meta, record){
	                meta.tdAttr = 'data-qtip="' + value + '"';
	                return value;
	            }
	        },*/ {
	            header: '备注',
	            dataIndex: 'c.remark',
	            width: 80,
	            renderer: function(value, meta, record){
	                meta.tdAttr = 'data-qtip="' + value + '"';
	                return value;
	            }
	        }];
    	} else {
	        this.columns = [{
	            header: '审核状态',
	            dataIndex: 'c.auditing_State',
	            width: 75,
	            //sortable: false,
	            renderer: function(v, meta, record){
	            	meta.style="padding-bottom: 0px;padding-top: 0px;line-height: 19px;padding-left: 0px;";
	                var text = "",tipText="";
	                if (v == 3) {
	                	tipText="报名待审核";
	                	text= '<div style="border-left:5px solid white;height:19px;padding-left:2px;">报名待审核</div>';
	                }
	                else if (v == 1) {
	                	text= '<div style="border-left:5px solid green;height:19px;padding-left:2px;">报名通过</div>';
	                	tipText="报名通过";
	                }else if (v == 2) {
	                	tipText="点击查看驳回原因";
	                	text= '<div style="border-left:5px solid red;height:19px;cursor: pointer;padding-left:2px;"><span style="cursor: pointer;color:blue;">报名未通过</span></div>';
	                }else if(v == 4){
	                	tipText="撤销待审核";
	                	text= '<div style="border-left:5px solid white;height:19px;padding-left:2px;">撤销待审核</div>';
	                }else if(v == 5){
	                	tipText="报名通过(撤销未通过)";
	                	text= '<div style="border-left:5px solid green;height:19px;padding-left:2px;">报名通过(<span style="cursor: pointer;color:blue;">撤销未通过</span>)</div>';
	                }
	                meta.tdAttr = 'data-qtip="' + tipText + '" auditStatus=' + record.get("c.auditing_State") + ' classUserId=' + record.get("classUserId") + ' userId=' + record.get("userId");
	                return text;
	            },
	    		listeners: {
	    			click: function(g, clm){
	//    				var userId = clm.getAttribute("userId");
	    				var auditStatus = clm.getAttribute("auditStatus");
	    				var classUserId = clm.getAttribute("classUserId");
	    				if(auditStatus == 5){
	    					var win = Ext.create('PJ.view.registrationmanagement.CheckEdit', {
	        					title: "查看驳回原因",
	        					flag:4,
	        					grid: me,
	        					isDisable:false,
	        					isReadOnly:true
	        				}).show();
	        				var actionName = "load";
	        				if(gj_onlineLearning_constant.loadType == "jsonp"){
	        					actionName = "loadJsonp";
	        				}
	        				win.down("form").getForm().doAction(actionName,{
	        					url:me.store.findRejectReasonUrl,
	        					params:{
	        						classUserId: classUserId
	        					},
	        					success:function(f,action){
	        					}
	        				});
	    				}else if(auditStatus == 2){
	    					var win = Ext.create('PJ.view.trainingmanagement.CheckEdit', {
	        					title: "查看驳回原因",
	        					flag:4,
	        					grid: me,
	        					isDisable:false,
	        					isReadOnly:true
	        				}).show();
	        				var actionName = "load";
	        				if(gj_onlineLearning_constant.loadType == "jsonp"){
	        					actionName = "loadJsonp";
	        				}
	        				win.down("form").getForm().doAction(actionName,{
	        					url:me.store.findCommentUrl,
	        					params:{
	        						classUserId: classUserId
	        					},
	        					success:function(f,action){
	        					}
	        				});
	    				}else{
	    					return;
	    				}
	    			}
	    		}
	        },{
	        	header: '学员帐号',
	        	dataIndex: 'bu.login_id',
	        	width: 75
	        },{
	        	header: '姓名',
	        	dataIndex: 'bu.user_Name',
	        	width: 70
	        }, {
	            header: '性别',
	            dataIndex: 'bb.order_num',
	            width: 45
	        },{
	            header: '出生年月',
	            dataIndex: 'c.birthday',
	            width: 70,
	            renderer: function(value, meta, record){
	                meta.tdAttr = 'data-qtip="' + value + '"';
	                return value;
	            }
	        }, {
	            header: '民族',
	            dataIndex: 'bt.order_num',
	            width: 50,
	            renderer: function(value, meta, record){
	                meta.tdAttr = 'data-qtip="' + value + '"';
	                return value;
	            }
	        },{
	            header: '手机号',
	            dataIndex: 'c.mobile_Telephone',
//	            hidden:true,
	            width: 100
	        }, {
	            header: '单位',
	            dataIndex: 'd.depname',
	            flex:1,
	            minWidth: 120,
	            renderer: function(value, meta, record){
	                meta.tdAttr = 'data-qtip="' + value + '"';
	                return value;
	            }
	        }, {
	            header: '职级',
	            dataIndex: 'bbb.order_num',
	            flex: .55,
	            renderer: function(value, meta, record){
	                meta.tdAttr = 'data-qtip="' + value + '"';
	                return value;
	            }
	        }, {
	            header: '单位及职务',
	            dataIndex: 'c.duty_remark',
	            flex:1,
//	            minWidth: 120,
	            renderer: function(value, meta, record){
	                meta.tdAttr = 'data-qtip="' + value + '"';
	                return value;
	            }
	        },/* {
	            header: '联系电话',
	            dataIndex: 'c.mobile_Telephone',
	            flex:1,
	            minWidth: 120,
	            renderer: function(value, meta, record){
	                meta.tdAttr = 'data-qtip="' + value + '"';
	                return value;
	            }
	        },*/ {
	            header: '备注',
	            dataIndex: 'c.remark',
	            width: 50,
	            renderer: function(value, meta, record){
	                meta.tdAttr = 'data-qtip="' + value + '"';
	                return value;
	            }
	        }];
    	}
        this.store = Ext.create('PJ.' + prototype + '.unitregistrationmanagement.ChooseUserList',{
            isLoad: false
        }),
        //实现排序序号的编辑
        this.plugins = [Ext.create('Ext.grid.plugin.CellEditing', {
        	clicksToEdit: 1,
            listeners: {
                beforeedit: function(editor, e){
                    var textfield = e.column.getEditor();
                    if(gj_onlineLearning_constant.loadType == "jsonp"){
                    	var storeToken = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
                    		loadType: gj_onlineLearning_constant.loadType,
                    		url: gj_onlineLearning_constant.tokenUrl
                    	});
                    	storeToken.load(function(rec, operation, success){
                    		var token = operation.response;
                    		textfield.token = token;
                    	});
                    }else if(gj_onlineLearning_constant.loadType == "ajax"){
                    	Ext.Ajax.request({
                            url: tokenUrl,
                            success: function(response){
                                var data = response.responseText;
                                var token = Ext.decode(data, true);
                                textfield.token = token;
                            }
                        });
                    }
                }
            }
        })];
        this.loadType = gj_onlineLearning_constant.loadType;
        this.callParent(arguments);
    },
    
    //报名
    onAddClick: function(button){
        var me = this;
        var win=Ext.create('PJ.view.registrationmanagement.ChooseUserEdit',{
    		title: "报名信息",
    		flag:1,
    		grid: me,
    		openState: me.up('window').openState,//培训班状态
    		classId: me.up('window').classId,//培训班Id
    		signUpState: me.signUpState//报名状态
    	});
        win.show();
      /*  win.on('close', function(){
        	
            window.location.reload() ;  
        });   */
        /*
        if(me.publishState == 2){
        	Ext.Msg.show({
                title: '提示',
                msg: '该班级未开始报名！',
                icon: Ext.Msg.WARNING,
                buttons: Ext.Msg.OK
            });
            return;
        }else if(me.openState == 1){
        	Ext.Msg.show({
                title: '提示',
                msg: '该班级已结束报名！',
                icon: Ext.Msg.WARNING,
                buttons: Ext.Msg.OK
            });
            return;
        }/*else if(me.signUpState == 2){
        	Ext.Msg.show({
                title: '提示',
                msg: '该班级未开始报名！',
                icon: Ext.Msg.WARNING,
                buttons: Ext.Msg.OK
            });
        	return;
        }else if(me.signUpState == 3){
        	Ext.Msg.show({
                title: '提示',
                msg: '该班级已结束报名！',
                icon: Ext.Msg.WARNING,
                buttons: Ext.Msg.OK
            });
        	return;
        }else{
        	
        }*/
    },
    //修改
    onUpdateClick: function(button){
        var me = this;
        var records = this.getSelectionModel().getSelection();
        if(records.length > 1){
			Ext.Msg.show({
				title: '提示',
				msg: '只允许操作一条数据！',
				icon: Ext.Msg.WARNING,
				buttons: Ext.Msg.OK
			});
			return;
		}
        /*
        if (records[0].get("c.auditing_State") == 1) {
        	Ext.Msg.show({
                title: '提示',
                msg: '不允许对审核通过的数据进行修改操作！',
                icon: Ext.Msg.WARNING,
                buttons: Ext.Msg.OK
            });
            return;
        }
		if (records[0].get("c.auditing_State") == 4) {
        	Ext.Msg.show({
                title: '提示',
                msg: '不允许对撤销待审核的数据进行修改操作！',
                icon: Ext.Msg.WARNING,
                buttons: Ext.Msg.OK
            });
            return;
        }
		if (records[0].get("c.auditing_State") == 5) {
        	Ext.Msg.show({
                title: '提示',
                msg: '不允许对报名通过(撤销未通过)的数据进行修改操作！',
                icon: Ext.Msg.WARNING,
                buttons: Ext.Msg.OK
            });
            return;
        }
        */
        var isJuji=records[0].get("bbb.order_num").indexOf('局级');
        var win = Ext.create('PJ.view.registrationmanagement.ChooseUserEdit',{
    		title: "修改报名信息",
    		flag:2,
    		grid: me,
    		userId:records[0].get("userId"),
    		openState: me.openState,//培训班状态
    		isJuji:isJuji!=-1?true:false,
    		classId: me.classId,//培训班Id
    		signUpState: records[0].get("c.auditing_State")//报名状态
    	}).show();
        win.down('form').getForm().load({
			url: me.store.preUpdateUrl,
            params: {
                searchUserId: records[0].get("userId"),
                searchClassID: me.up('window').classId,//培训班Id
                searchDepId:records[0].get("deptId"),
                deptName:records[0].get("d.depname"),
            },
            success: function(baseForm, action){
//                baseForm.findField("tpassword").setValue(action.result.data.password);
            }
        });
        /*
        Ext.Msg.confirm({
            title: '提示',
            msg: '您确定要修改这些信息吗?',
            buttons: Ext.Msg.OKCANCEL,
            fn: function(id){
                if (id == 'ok') {
                    var classUserIds = new Array();
                    Ext.Array.each(records, function(name, index){
                    	classUserIds.push(name.get("classUserId"));
                    });
                    if(gj_onlineLearning_constant.loadType == "jsonp"){
                    	var store1 = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
                    		loadType: gj_onlineLearning_constant.loadType,
                    		url: me.getStore().updateUrl,
                    		extraParams: {
                    			searchClassUserIds: classUserIds,
                    			openState: me.up('window').openState
                    		}
                    	});
                    	store1.load(function(rec, operation, success){
                    		var msg = success == true ? '修改成功！' : operation.error;
                			Orchid.alert('消息', msg, function(){
            					var store = me.getStore();
            					store.load();
            				});
                    	});
                    }else if(gj_onlineLearning_constant.loadType == "ajax"){
                    	Ext.Ajax.request({
                    		url: me.getStore().updateUrl,
                    		params: {
                    			searchClassUserIds: classUserIds,
                    			openState: me.up('window').openState
                    		},
                    		success: function(response){
                    			var data = response.responseText;
                    			var json = Ext.decode(data, true);
                        		var msg = json.success == true ? '修改成功！' : json.msg;
                    			Orchid.alert('消息', msg, function(){
                					var store = me.getStore();
                					store.load();
                				});
                    		}
                    	});
                    }
                }
                else {
                    return;
                }
            }
        });
        */
    },
    //撤销报名
    onUnSignUpClick: function(button){
        var me = this;
        var records = this.getSelectionModel().getSelection();
        var flag=true;
        Ext.Array.each(records, function(name, index){
        	if(me.clickSource==2&&name.get("c.auditing_State")==4){
        		Orchid.alert('消息', '撤销待审核的学员不允许撤销！');
        		flag=false;
        	}
        });
        if(flag){
        	Ext.Msg.confirm({
        		title: '提示',
        		msg: '您确定要为这些学员撤销报名吗？',
        		buttons: Ext.Msg.OKCANCEL,
        		fn: function(id){
        			if (id == 'ok') {
        				var classUserIds = new Array();
        				var userIds = new Array();
        				
        				Ext.Array.each(records, function(name, index){
        					classUserIds.push(name.get("classUserId"));
        					userIds.push(name.get("userId"));
        				});
        				Ext.Ajax.request({
        					url: me.getStore().unSignUpUrl,
        					params: {
        						searchClassUserIds: classUserIds,
        						searchUserIds: userIds,
        						auditingState:me.auditingState,
        						clickSource:me.clickSource,
        						openState: me.up('window').openState
        					},
        					success: function(response){
        						var data = response.responseText;
        						var json = Ext.decode(data, true);
        						var msg = json.success == true ? '操作成功！' : json.msg;
        						Orchid.alert('消息', msg, function(){
        							var store = me.getStore();
        							store.load();
        							//me.quotaOfPeople();//预授人数统计
        						});
        						
        						//刷新已选列表
        						var gridList = me.up("container").down("ChooseNoUserList");
        						var gridListStore = gridList.getStore();
        						gridListStore.reload();
        					}
        				});
        			}else {
        				return;
        			}
        		}
        	});
        }
    },
    /**
     * 导出
     */
    onExportClick: function(num){
    	var me = this;
    	var store = me.getStore(); 
    	var href=store.exportUserPaperNumExcelUrl + "?" + "&searchClassID=" + me.up('window').classId + '&clickSource='+me.clickSource;
    	href = encodeURI(href);
    	var atag = Ext.get("exportResume");
    	atag.set({
    		"href": href
    	});
    	atag.dom.click();
    },
    //班级人员数量统计
    quotaOfPeople: function(){
    	var me = this;
    	var recordId = me.recordId;
        var quotaOfPeopleLabel = this.down("#unitQuotaOfPeople");
        var classId = me.classId;
        if (quotaOfPeopleLabel) {
        	if(recordId == 1){
        		quotaOfPeopleLabel.setText("");
        	}else{
        		if(gj_onlineLearning_constant.loadType == "jsonp"){
                	var store1 = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
                		loadType: gj_onlineLearning_constant.loadType,
                		url: me.store.unitQuotaOfPeopleUrl + '?searchClassId=' + classId,
                		extraParams: {
                			searchDepId: me.recordId
                		}
                	});
                	store1.load(function(rec, operation, success){
                		if(success){
                			var jsonData = operation.response.result;
                			quotaOfPeopleLabel.setText(
                					'可报<span style="color:red">' + jsonData.asignCount +
                					'</span>人（市级<span style="color:red">' + jsonData.asignCityCount +
                					'</span>人，县级<span style="color:red">' + jsonData.asignCountyCount + 
                					'</span>人），已报<span style="color:red">' + jsonData.reportedCount +
                					'</span>人（市级<span style="color:red">' + jsonData.reportedCityCount +
                					'</span>人，县级<span style="color:red">' + jsonData.reportedCountyCount +
                					'</span>人），剩余<span style="color:red">' + jsonData.surplusCount +
                					'</span>人', false);
                		}
                	});
                }else if(gj_onlineLearning_constant.loadType == "ajax"){
                	Ext.Ajax.request({
                		url: me.store.unitQuotaOfPeopleUrl + '?searchClassId=' + classId,
                		params: {
                			searchDepId: me.recordId
                    	},
                		success: function(response){
                			var data = response.responseText;
                			var json = Ext.decode(data, true);
                			if (json.success) {
                				var jsonData = json.data;
                				quotaOfPeopleLabel.setText(//可报100人（市级80人，县级20人），已报80人(市级60人，县级20人)，剩余20人
                						'可报<span style="color:red">' + jsonData.asignCount +
                    					'</span>人（市级<span style="color:red">' + jsonData.asignCityCount +
                    					'</span>人，县级<span style="color:red">' + jsonData.asignCountyCount + 
                    					'</span>人），已报<span style="color:red">' + jsonData.reportedCount +
                    					'</span>人（市级<span style="color:red">' + jsonData.reportedCityCount +
                    					'</span>人，县级<span style="color:red">' + jsonData.reportedCountyCount +
                    					'</span>人），剩余<span style="color:red">' + jsonData.surplusCount +
                    					'</span>人', false);
                			}
                		}
                	});
                }
        	}
        }
    },
    listeners: {
        //点击列表数据时显示按钮事件
    	selectionchange: function(selModel, selected){
    		this.down('#' + this.oldId + "UnSignUp").setDisabled(selected.length === 0);//撤销报名
    	},
        viewready: function(){
            this.addDocked([{
                xtype: 'toolbar',
                dock: 'top',
                displayInfo: true,
                items: [/*{
                    text: '新增',
                    iconCls: 'addIco',
                    itemId: this.oldId + "Add",
                    disabled: false,
                    scope: this,
                    handler: this.onAddClick
                }, "-", {
                    text: '修改',
                    iconCls: 'signUpUpdateIco',
                    itemId: this.oldId + "Update",
                    disabled: true,
                    scope: this,
                    handler: this.onUpdateClick
                }, "-",*/ {
                    text: '撤销',
                    iconCls: 'publishCancelIco',
                    itemId: this.oldId + "UnSignUp",
                    disabled: true,
                    scope: this,
                    handler: this.onUnSignUpClick
                }, "-",{
                    text: '导出',
                    iconCls: 'entryListIco',
                    itemId: this.oldId + "Export",
                    disabled: false,
                    scope: this,
                    handler: this.onExportClick
                }, "-", {
                    xtype: 'label',
                    itemId: 'unitQuotaOfPeople',
        			labelAlign: 'right',
                    html: '<span style="color:red;padding-left: 160px;">请仔细核对参学人员信息，如有调整请点击修改按钮</span>'
                }, "->", "->", this.countSelectPanel]
            }]);
            // 触发权限过滤事件
            this.fireEvent("permission", this);
            //树节点为7382时,“报名”按钮才可用
        	/*var addButton = this.down('#' + this.oldId + "Add");
        	var buttonFlag = this.recordId != 1?false:true;
			addButton.setDisabled(buttonFlag);*/
//            this.quotaOfPeople();//预授人数统计
        }
    }
});
